package com.xncoding.easypoi.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 设置页面附加属性
 *
 */
public class PDFBuilder extends PdfPageEventHelper {

	private static Logger log= LoggerFactory.getLogger(PDFBuilder.class);

	//字体文件名
	private String fontFileName;
	// 基础字体对象
	private  BaseFont bf;
	// 利用基础字体生成的字体对象，一般用于生成中文文字
	private  Font fontDetail;
	//文档字体大小
	private  int fontSize=12;
	//模板
	private  PdfTemplate template;
	//数据实体
	private  Object data;
	//页眉页脚定制接口
	private PDFHeaderFooter PDFHeaderFooter;

	//不允许空的构造方法
	@SuppressWarnings("unused")
	private PDFBuilder() {

	}

	public PDFBuilder(PDFHeaderFooter PDFHeaderFooter) {
		this(PDFHeaderFooter,null);
	}

	public PDFBuilder(PDFHeaderFooter PDFHeaderFooter, Object data) {
		this(PDFHeaderFooter,data,"ping_fang_light.ttf");
	}
	public PDFBuilder(PDFHeaderFooter PDFHeaderFooter, Object data, String fontFileName) {
		this(PDFHeaderFooter,data,fontFileName,12);
	}
	public PDFBuilder(PDFHeaderFooter PDFHeaderFooter, Object data, int fontSize) {
		this(PDFHeaderFooter,data,"ping_fang_light.ttf",fontSize);
	}

	public PDFBuilder(PDFHeaderFooter PDFHeaderFooter, Object data, String fontFileName, int fontSize) {
		this.PDFHeaderFooter = PDFHeaderFooter;
		this.data=data;
		this.fontFileName=fontFileName;
		this.fontSize=fontSize;
	}

	/**
	 * 打开文档时获取模板
	 */
	public void onOpenDocument(PdfWriter writer, Document document) {
		template = writer.getDirectContent().createTemplate(50, 50);
	}
	/**
	 *
	 * 关闭每页的时候，写入页眉,页脚等
	 *
	 */
	public void onEndPage(PdfWriter writer, Document document) {
		//1.初始化字体
		initFront();
		if(PDFHeaderFooter !=null){
			//2.写入页眉
			PDFHeaderFooter.writeHeader(writer,document,data,fontDetail,template);
			//3.写入前半部分页脚
			PDFHeaderFooter.writeFooter(writer,document,data,fontDetail,template);
		}
	}

	/**
	 * 关闭文档时，替换模板，完成整个页眉页脚组件
	 */
	public void onCloseDocument(PdfWriter writer, Document document) {
		if(PDFHeaderFooter !=null){
			template.beginText();
			template.setFontAndSize(bf,fontSize);
			String replace= PDFHeaderFooter.getReplaceOfTemplate(writer,document,data);
			template.showText(replace);
			template.endText();
			template.closePath();
		}
	}

	/**
	 * @description 初始化字体
	 */
	private void initFront(){
		if(StringUtils.isEmpty(fontFileName)){
			log.error("PDF文档字体设置错误");
			return;
		}
		try {
			if (bf == null) {
				//添加字体，以支持中文
				String classpath=PDFBuilder.class.getClassLoader().getResource("").getPath();
				String fontPath =classpath+"fonts/"+fontFileName;
				//创建基础字体
				bf = BaseFont.createFont(fontPath,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
			}
			if (fontDetail == null) {
				fontDetail = new Font(bf, fontSize, Font.NORMAL);// 数据体字体
				log.info("PDF文档字体初始化完成!");
			}
		} catch (Exception e) {
			log.error("字体初始化失败{}", ExceptionUtils.getFullStackTrace(e));
		}
	}
}